package com.fenshi.manager.activity.backcar;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fenshi.bean.OrderBean;
import com.fenshi.manager.R;
import com.fenshi.manager.activity.order.DamageOrderActivty;
import com.fenshi.presenter.BackCarPresenter;
import com.fenshi.presenter.imp.BackCarPresenterImp;
import com.fenshi.until.CookicUntil;
import com.fenshi.view.BackView;
import com.quickdv.activity.BaseActivity;

import butterknife.InjectView;
import zxing.activity.CaptureActivity;


public class BackCarOrderActivity extends BaseActivity implements View.OnClickListener, BackView {

    Button back, lock;
    BackCarPresenter presenter;
    ProgressDialog loading;
    public static int BACKORDER = 300;
    int type = 0;
    String path;
    View detail;
    @InjectView(R.id.text1)
    TextView text1;
    @InjectView(R.id.order_code)
    TextView orderCode;
    @InjectView(R.id.text2)
    TextView text2;
    @InjectView(R.id.order_user)
    TextView orderUser;
    @InjectView(R.id.text4)
    TextView text4;
    @InjectView(R.id.order_type)
    TextView orderType;
    @InjectView(R.id.order_create_time)
    TextView orderCreateTime;
    @InjectView(R.id.order_back_car_time)
    TextView orderBackCarTime;
    @InjectView(R.id.order_car_distance)
    TextView orderCarDistance;
    @InjectView(R.id.order_consume_time)
    TextView orderConsumeTime;
    @InjectView(R.id.order_cost)
    TextView orderCost;
    @InjectView(R.id.car)
    TextView car;
    @InjectView(R.id.order_car_detail_linear)
    RelativeLayout orderCarDetailLinear;
    @InjectView(R.id.lock_car)
    TextView lockCar;
    @InjectView(R.id.order_lock_car_detail_linear)
    RelativeLayout orderLockCarDetailLinear;
    @InjectView(R.id.back_car)
    Button backCar;

    @Override
    public int getLayoutID() {
        return R.layout.activity_back_car_order;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initActionbar() {

    }

    @Override
    public void initData() {
        presenter = new BackCarPresenterImp(this);
        presenter.initViewAndEvent();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.order_car_detail_linear:
                Intent intent = new Intent(BackCarOrderActivity.this, DamageOrderActivty.class);
                startActivityForResult(intent, BACKORDER);
                break;
            case R.id.back_car:
                new AlertDialog.Builder(BackCarOrderActivity.this).setTitle("还车确认").setMessage("点击确定后锁定车辆").setPositiveButton(getString(R.string.sure), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
//                        lock.setBackgroundColor(getResources().getColor(R.color.gray_bg));
//                        lock.setOnClickListener(null);
                        presenter.checkCar(type);
                    }
                }).setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialog.dismiss();
                    }
                }).create().show();
                break;
            case R.id.order_lock_car_detail_linear:
                Intent intent1 = new Intent(BackCarOrderActivity.this, CaptureActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString(CookicUntil.FROM, "backOrderActivity");
                intent1.putExtras(bundle);
                startActivityForResult(intent1,BACKORDER);
                break;
        }
    }

    @Override
    public void setBackLiearShow(int isShow) {
        orderLockCarDetailLinear.setVisibility(isShow);
    }

    @Override
    public void setOrderDetailShow(int isShow) {
        orderConsumeTime.setVisibility(isShow);
    }

    @Override
    public void setEumShow(int isShow) {

    }

    @Override
    public void refreshView(OrderBean orderBean) {
        orderCode.setText(orderBean.getOrderCode());
        orderUser.setText(orderBean.getUserName());
        orderCreateTime.setText(orderBean.getCreateTime());
        orderBackCarTime.setText(orderBean.getEndTime());
        orderCarDistance.setText(orderBean.getDistance() + "");
        orderConsumeTime.setText(orderBean.getOrderTime());
        orderCost.setText(orderBean.getMoney() + "");
    }

    @Override
    public void showLoad() {
        if (loading != null && !loading.isShowing()) loading.show();
    }

    @Override
    public void dismissLoad() {
        if (loading != null && loading.isShowing()) loading.dismiss();
    }

    @Override
    public void showError(String error) {
        Toast.makeText(this, error, Toast.LENGTH_LONG).show();
    }

    @Override
    public void initViewAndEvnet() {
        back = findView(R.id.back_car);
        lock = findView(R.id.order_lock_car_detail_linear);
        detail = findView(R.id.order_car_detail_linear);

        back.setOnClickListener(this);
        lock.setOnClickListener(this);
        detail.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == BackActivity.BACKCAR){
            if (resultCode == BackActivity.BACKCAR){
                path = data.getExtras().getString("pic");
                type = 1;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
